﻿Imports TMySQL
Public Class frmCreateIndex
    Private _Table As IHasColumns
    Private _List As New List(Of frmColumn)
    Private Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
    End Sub
    Public Sub New(ByVal ATable As IHasColumns)
        Me.New()
        Me.DialogResult = Windows.Forms.DialogResult.None
        _Table = ATable
        AddRow()
    End Sub

    Private Sub DeleteRow()
        Dim idx As Integer = _List.Count - 1
        If idx < 0 Then
            Return
        End If
        _List(idx).Dispose()
        _List.RemoveAt(idx)
    End Sub
    Private Sub AddRow()
        Dim frmRet As New frmColumn(_Table.Columns)
        _List.Add(frmRet)
        frmRet.TopLevel = False
        frmRet.Dock = DockStyle.Top
        frmRet.Parent = pnl
        frmRet.Show()
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        DeleteRow()
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        AddRow()
    End Sub

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub
    Private Sub ValidateEntries()
        If _List.Count <= 0 Then
            Throw New Exception("列を１つ以上指定してください")
        End If
    End Sub
    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        Dim SQL As String = CreateIndexSQL
        If Confirm(SQL) <> Windows.Forms.DialogResult.OK Then
            Return
        End If
        Dim myQuery As New TMyQuery
        myQuery.Session = frmMain.Connection
        Cursor.Current = Cursors.WaitCursor
        Try
            myQuery.SQL.Append(SQL)
            myQuery.Execute()
            Call MsgBox("インデックスを作成しました", MsgBoxStyle.Information)
            Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()
        Catch ex As Exception
            Call MsgBox("エラーが発生しました。" & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
        Finally
            Cursor.Current = Cursors.Default
            myQuery.Dispose()
        End Try
    End Sub
    Private Function Confirm(ByVal ASQL As String) As DialogResult
        Dim frmRet As New frmConfirmMsg(ASQL)
        Try
            Return frmRet.ShowDialog
        Finally
            frmRet.Dispose()
        End Try
    End Function
    Private ReadOnly Property CreateIndexSQL() As String
        Get
            Dim isFirst As Boolean = True
            Dim sb As New System.Text.StringBuilder
            sb.Append("ALTER TABLE `").Append(_Table.TableSchema).Append("`.`").Append(_Table.TableName).AppendLine("`")
            If cboIndexKind.SelectedItem.Equals("PRIMARY") Then
                sb.Append("ADD PRIMARY KEY USING ").AppendLine(cboIndexType.SelectedItem)
            ElseIf cboIndexKind.SelectedItem.Equals("UNIQUE") Then
                sb.AppendLine("ADD UNIQUE ").Append(txtIndexName.Text).Append(" USING ").AppendLine(cboIndexType.SelectedItem)
            ElseIf cboIndexKind.SelectedItem.Equals("INDEX") Then
                sb.Append("ADD INDEX ").Append(txtIndexName.Text).Append(" USING ").AppendLine(cboIndexType.SelectedItem)
            ElseIf cboIndexKind.SelectedItem.Equals("FULLTEXT") Then
                sb.Append("ADD FULLTEXT  ").Append(txtIndexName.Text).Append(" USING ").AppendLine(cboIndexType.SelectedItem)
            End If
            sb.Append("(")
            For Each frm As frmColumn In _List
                If isFirst Then
                    sb.AppendLine(frm.ColumnSQL)
                Else
                    sb.Append(",").AppendLine(frm.ColumnSQL)
                End If
            Next
            sb.Append(")")
            Return sb.ToString
        End Get
    End Property

    Private Sub cboIndexType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboIndexKind.SelectedIndexChanged
        If cboIndexKind.SelectedItem.Equals("PRIMARY") Then
            txtIndexName.Text = "PRIMARY"
        End If
    End Sub
End Class